package com.RyanRestaurant.Utils;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具类
 */

@Data
public class PageUtils<T> {

    private Integer currentPage; //当前页面
    private Integer pageSize; //每一页的大小
    private Integer pageCount; //总页数
    private Integer totalCount; //总条数

    private List<T> records; //每页数据

    private List<Integer> numbers=new ArrayList<>(); //用户的分页列表

    private Integer startPage = 1; //起始页码

    private Integer endPage; //结束页码

    //构造函数
    public PageUtils(Integer pageSize, Integer currentPage, Integer totalCount, List<T> records)
    {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalCount = totalCount;
        this.records = records;
        this.pageCount = totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize) + 1;

        if (this.pageCount <= 9)
        {
            this.startPage = 1;
            this.endPage = pageCount;
        }else {
            this.startPage = this.currentPage - 4;
            this.endPage = this.currentPage + 4;

            //如果起始页码小于1
            if (this.startPage < 1)
            {
                this.startPage = 1;
                this.endPage = 9;
            }else if (this.endPage > this.pageCount)
            {
                this.endPage = this.pageCount;
                this.startPage = this.pageCount - 8;
            }
        }

        for (int i = this.startPage; i <= this.endPage; i++)
        {
            numbers.add(i);
        }
    }
}
